﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFCons
{
    public partial class BoxSelectMark : UserControl
    {
        public EventHandler ButtonClicked;
        public BoxSelectMark()
        {
            InitializeComponent();
            buttonBoxSelect.Tag = 0;
            buttonTest.Tag = 1;
            labelMarkFn.Tag = 0;

            buttonBoxSelect.Click += (s, e) =>
            {
                if (ButtonClicked != null) ButtonClicked(s, e);

            };
            buttonTest.Click += (s, e) =>
              {
                  if (ButtonClicked != null) ButtonClicked(s, e);
              };
        }

        public static Image From(string fn)
        {
            var fs = new System.IO.FileStream(fn, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            var img = Image.FromStream(fs);
            fs.Close();
            if (img.PixelFormat == PixelFormat.Format8bppIndexed) img.Palette = CreateGrayPalette();
            return img;
        }

        public static ColorPalette CreateGrayPalette()
        {
            ColorPalette palette;
            using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
            {
                palette = bmp.Palette;
            }
            for (int i = 0; i < 256; i++)
            {
                palette.Entries[i] = Color.FromArgb(i, i, i);
            }
            return palette;
        }

        public string InitialDir = Application.StartupPath;
        public string DefaultFileName = "default";
        private void buttonLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Image File(*.jpg,*.bmp,*.png)|*.jpg;*.bmp;*.png|All file(*.*)|*.*";
            //dlg.InitialDirectory = GlobalParams.CD.MarkPath;
            //dlg.FileName = Path.GetFileNameWithoutExtension(GlobalParams.CD.recipe);
            dlg.InitialDirectory = InitialDir;
            dlg.FileName = DefaultFileName;
            dlg.Multiselect = false;
            dlg.CheckPathExists = true;
            dlg.CheckFileExists = true;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    string Fn_Mark = dlg.FileName;
                    pictureBoxMark.Image = From(Fn_Mark);
                    labelMarkFn.Text = dlg.FileName;
                    labelMarkFn.Tag = 1;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "ERROR", ex.Message);
                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            //dlg.InitialDirectory = GlobalParams.CD.MarkPath;
            dlg.InitialDirectory = InitialDir;
            dlg.Filter = "Image File(*.jpg,*.bmp,*.png)|*.jpg;*.bmp;*.png|All file(*.*)|*.*";
            dlg.DefaultExt = "bmp";
            //dlg.FileName = Path.GetFileNameWithoutExtension(GlobalParams.CD.recipe);
            dlg.FileName = DefaultFileName;
            dlg.AddExtension = true;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string dst = dlg.FileName;
                pictureBoxMark.Image.Save(dst);
                labelMarkFn.Text = dst;
                labelMarkFn.Tag = 1;
            }
        }

        public void SetSelectedMark(Image mark, string mark_label)
        {
            pictureBoxMark.Image = mark;
            labelMarkFn.Text = mark_label;
            labelMarkFn.Tag = 2;
        }

        public void SetMarkFilename(string fn)
        {
            if (string.IsNullOrEmpty(fn))
            {
                labelMarkFn.Text = "NO MARK";
                labelMarkFn.Tag = 0;
                pictureBoxMark.Image = null;
                return;
            }

            if (File.Exists(fn) == false)
            {
                labelMarkFn.Text = "NO MARK FILE";
                labelMarkFn.Tag = 0;
                pictureBoxMark.Image = null;
                MessageBox.Show(this, "MARK文件不存在" + Environment.NewLine + fn, "ERROR");
                return;
            }

            try
            {
                var img = From(fn);
                pictureBoxMark.Image = img;
                labelMarkFn.Text = fn;
                labelMarkFn.Tag = 1;
            }
            catch (Exception ex)
            {
                labelMarkFn.Text = "ERROR:" + fn;
                labelMarkFn.Tag = 0;
                pictureBoxMark.Image = null;
                MessageBox.Show(this, "加载MARK文件失败" + Environment.NewLine + fn + Environment.NewLine + ex.Message, "ERROR");
            }
        }

        public Image Image =>pictureBoxMark.Image;

  //      public bool TemplateEnable
		//{
		//	get { return checkBoxEnable.Checked; }
		//	set { checkBoxEnable.Checked = value; }
		//}
		public string GetMarkFile()
		{
            int tag = Convert.ToInt32(labelMarkFn.Tag);
            if (tag == 0)//no assign mark
            {
                return "";
            }
            else if (tag == 1)//OK
            {
                return labelMarkFn.Text;
            }
            else if (tag == 2)//selected
            {
                return "NOSAVE";
            }
            return "";
        }
    }
}
